﻿Ext.gesture.Pinch = Ext.extend(Ext.gesture.Gesture, {
    handles: [
        'pinchstart',
        'pinch',
        'pinchend'
    ],

    touches: 2,

    onTouchStart: function (e) {
        var me = this;

        if (Ext.supports.Touch && e.targetTouches.length >= 2) {
		
			//锁住下面事件不让触发
            me.lock('swipe', 'scroll', 'scrollstart', 'scrollend', 'touchmove', 'touchend', 'touchstart', 'tap', 'tapstart', 'taphold', 'tapcancel', 'doubletap');
            me.pinching = true;//pinching标志设为true

            var targetTouches = e.targetTouches,
                firstTouch = me.firstTouch = targetTouches[0],
                secondTouch = me.secondTouch = targetTouches[1];

            me.previousDistance = me.startDistance = me.getDistance();//返回两点的距离
            me.previousScale = 1;

			//触发pinchstart事件
            me.fire('pinchstart', e, {
                distance: me.startDistance,
                scale: me.previousScale
            });
        }
		//如果pinching标志为true
        else if (me.pinching) {
			//解锁
            me.unlock('swipe', 'scroll', 'scrollstart', 'scrollend', 'touchmove', 'touchend', 'touchstart', 'tap', 'tapstart', 'taphold', 'tapcancel', 'doubletap');
            //pinching标志设为false
			me.pinching = false;
        }
    },

    onTouchMove: function (e) {
		//如果pinching标志为true
        if (this.pinching) {
            this.fire('pinch', e, this.getPinchInfo());//触发pinch事件
        }
    },

    onTouchEnd: function (e) {
		//如果pinching标志为true
        if (this.pinching) {
            this.fire('pinchend', e, this.getPinchInfo());//触发pinchend事件
        }
    },

	//获取pinch信息
    getPinchInfo: function () {
        var me = this,
            distance = me.getDistance(),//两点的距离
            scale = distance / me.startDistance,//当前距离与初始距离的比例
            firstTouch = me.firstTouch,//第一个触发点
            secondTouch = me.secondTouch,//第二个触发点
            info = {
                scale: scale,//比例
                deltaScale: scale - 1,//比例差（绝对差）
                previousScale: me.previousScale,//前一比例
                previousDeltaScale: scale - me.previousScale,//比例与前一比例的差（相对差）
                distance: distance,//两点现在的距离
                deltaDistance: distance - me.startDistance,//两点现在的距离与开始距离的差（绝对差）
                startDistance: me.startDistance,//两点开始距离
                previousDistance: me.previousDistance,//两点前一距离
                previousDeltaDistance: distance - me.previousDistance,//两点现在距离与前一距离的差（相对差）
                firstTouch: firstTouch,//第一个触发点
                secondTouch: secondTouch,//第二个触发点
                firstPageX: firstTouch.pageX,//第一个触发点的横坐标
                firstPageY: firstTouch.pageY,//第一个触发点的纵坐标
                secondPageX: secondTouch.pageX,//第二个触发点的横坐标
                secondPageY: secondTouch.pageY,//第二个触发点点纵坐标

                midPointX: (firstTouch.pageX + secondTouch.pageX) / 2,//两点的中点横坐标
                midPointY: (firstTouch.pageY + secondTouch.pageY) / 2//两点的中点纵坐标
            };

        me.previousScale = scale;//当前比例变为前一比例
        me.previousDistance = distance;//当前距离变为前一距离

		//返回pinch信息对象
        return info;
    },

	//返回两点的距离
    getDistance: function () {
        var me = this;
		
		//返回三角形两边平方和的平方跟，等于第三边的长度
        return Math.sqrt(
            Math.pow(Math.abs(me.firstTouch.pageX - me.secondTouch.pageX), 2) +//横坐标差绝对值的平方
            Math.pow(Math.abs(me.firstTouch.pageY - me.secondTouch.pageY), 2)//众坐标差的绝对值的平方
        );
    }
});

Ext.regGesture('pinch', Ext.gesture.Pinch);